/*     */ package com.zimbra.cs.ldap.unboundid;
/*     */ 
/*     */ import com.unboundid.asn1.ASN1OctetString;
/*     */ import com.unboundid.ldap.sdk.Modification;
/*     */ import com.unboundid.ldap.sdk.ModificationType;
/*     */ import com.zimbra.common.util.StringUtil;
/*     */ import com.zimbra.cs.account.Entry;
/*     */ import com.zimbra.cs.ldap.LdapUtil;
/*     */ import com.zimbra.cs.ldap.ZLdapElement.ZLdapElementDebugListener;
/*     */ import com.zimbra.cs.ldap.ZModificationList;
/*     */ import java.util.ArrayList;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import java.util.Map.Entry;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class UBIDModificationList
/*     */   extends ZModificationList
/*     */ {
/*  34 */   private List<Modification> modList = new ArrayList();
/*     */   
/*     */   public void debug(ZLdapElement.ZLdapElementDebugListener debugListener)
/*     */   {
/*  38 */     for (Modification mod : this.modList) {
/*  39 */       print(debugListener, mod.toString() + " ,");
/*     */     }
/*     */   }
/*     */   
/*     */   List<Modification> getModList() {
/*  44 */     return this.modList;
/*     */   }
/*     */   
/*     */   void replaceAll(Map<String, Object> attrs)
/*     */   {
/*  49 */     for (Map.Entry<String, Object> attr : attrs.entrySet()) {
/*  50 */       String attrName = (String)attr.getKey();
/*  51 */       Object attrValue = attr.getValue();
/*     */       
/*  53 */       Modification mod = null;
/*  54 */       if (attrValue == null) {
/*  55 */         mod = new Modification(ModificationType.DELETE, attrName);
/*  56 */       } else if ((attrValue instanceof String)) {
/*  57 */         if (((String)attrValue).isEmpty()) {
/*  58 */           mod = new Modification(ModificationType.DELETE, attrName);
/*     */         } else {
/*  60 */           mod = new Modification(ModificationType.REPLACE, attrName, (String)attrValue);
/*     */         }
/*  62 */       } else if ((attrValue instanceof String[])) {
/*  63 */         mod = new Modification(ModificationType.REPLACE, attrName, (String[])attrValue);
/*     */       }
/*     */       
/*  66 */       this.modList.add(mod);
/*     */     }
/*     */   }
/*     */   
/*     */   public boolean isEmpty()
/*     */   {
/*  72 */     return this.modList.size() == 0;
/*     */   }
/*     */   
/*     */ 
/*     */   public void addAttr(String name, String[] value, Entry entry, boolean containsBinaryData, boolean isBinaryTransfer)
/*     */   {
/*  78 */     String[] currentValues = entry.getMultiAttr(name, false);
/*     */     
/*  80 */     List<ASN1OctetString> valuesToAdd = null;
/*  81 */     for (int i = 0; i < value.length; i++)
/*  82 */       if (!LdapUtil.contains(currentValues, value[i]))
/*     */       {
/*     */ 
/*  85 */         if (valuesToAdd == null) {
/*  86 */           valuesToAdd = new ArrayList();
/*     */         }
/*  88 */         valuesToAdd.add(UBIDUtil.newASN1OctetString(containsBinaryData, value[i]));
/*     */       }
/*  90 */     if (valuesToAdd != null) {
/*  91 */       String transferAttrName = LdapUtil.attrNameToBinaryTransferAttrName(isBinaryTransfer, name);
/*  92 */       Modification mod = new Modification(ModificationType.ADD, transferAttrName, (ASN1OctetString[])valuesToAdd.toArray(new ASN1OctetString[valuesToAdd.size()]));
/*     */       
/*     */ 
/*  95 */       this.modList.add(mod);
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */   public void modifyAttr(String name, String value, Entry entry, boolean containsBinaryData, boolean isBinaryTransfer)
/*     */   {
/* 102 */     ModificationType modOp = StringUtil.isNullOrEmpty(value) ? ModificationType.DELETE : ModificationType.REPLACE;
/* 103 */     if (modOp == ModificationType.DELETE)
/*     */     {
/* 105 */       if (entry.getAttr(name, false) == null) {
/* 106 */         return;
/*     */       }
/*     */     }
/*     */     
/* 110 */     if (modOp == ModificationType.DELETE) {
/* 111 */       removeAttr(name, isBinaryTransfer);
/*     */     } else {
/* 113 */       String[] val = { value };
/* 114 */       modifyAttr(name, val, containsBinaryData, isBinaryTransfer);
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public void modifyAttr(String name, String[] value, boolean containsBinaryData, boolean isBinaryTransfer)
/*     */   {
/* 122 */     List<ASN1OctetString> valuesToMod = new ArrayList();
/* 123 */     for (int i = 0; i < value.length; i++) {
/* 124 */       valuesToMod.add(UBIDUtil.newASN1OctetString(containsBinaryData, value[i]));
/*     */     }
/*     */     
/* 127 */     String transferAttrName = LdapUtil.attrNameToBinaryTransferAttrName(isBinaryTransfer, name);
/* 128 */     Modification mod = new Modification(ModificationType.REPLACE, transferAttrName, (ASN1OctetString[])valuesToMod.toArray(new ASN1OctetString[valuesToMod.size()]));
/*     */     
/*     */ 
/* 131 */     this.modList.add(mod);
/*     */   }
/*     */   
/*     */ 
/*     */   public void removeAttr(String attrName, boolean isBinaryTransfer)
/*     */   {
/* 137 */     String transferAttrName = LdapUtil.attrNameToBinaryTransferAttrName(isBinaryTransfer, attrName);
/* 138 */     Modification mod = new Modification(ModificationType.DELETE, transferAttrName);
/* 139 */     this.modList.add(mod);
/*     */   }
/*     */   
/*     */ 
/*     */   public void removeAttr(String name, String[] value, Entry entry, boolean containsBinaryData, boolean isBinaryTransfer)
/*     */   {
/* 145 */     String[] currentValues = entry.getMultiAttr(name, false);
/* 146 */     if ((currentValues == null) || (currentValues.length == 0)) {
/* 147 */       return;
/*     */     }
/*     */     
/* 150 */     List<ASN1OctetString> valuesToRemove = null;
/* 151 */     for (int i = 0; i < value.length; i++)
/* 152 */       if (LdapUtil.contains(currentValues, value[i]))
/*     */       {
/*     */ 
/* 155 */         if (valuesToRemove == null) {
/* 156 */           valuesToRemove = new ArrayList();
/*     */         }
/* 158 */         valuesToRemove.add(UBIDUtil.newASN1OctetString(containsBinaryData, value[i]));
/*     */       }
/* 160 */     if (valuesToRemove != null) {
/* 161 */       String transferAttrName = LdapUtil.attrNameToBinaryTransferAttrName(isBinaryTransfer, name);
/* 162 */       Modification mod = new Modification(ModificationType.DELETE, transferAttrName, (ASN1OctetString[])valuesToRemove.toArray(new ASN1OctetString[valuesToRemove.size()]));
/*     */       
/* 164 */       this.modList.add(mod);
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/ldap/unboundid/UBIDModificationList.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */